package cn.ljp.userLike.service.service.impl;

import cn.common.beancopy.util.BeanCopierUtil;
import cn.common.redis.RedisServie;
import cn.hutool.core.lang.generator.ObjectIdGenerator;
import cn.ljp.userLike.mapper.mapper.SmallMiGoodsSkuMapper;
import cn.ljp.userLike.mapper.mapper.SmallMiGoodsSpuMapper;
import cn.ljp.userLike.mapper.mapper.SmallMiUserLikeMapper;
import cn.ljp.userLike.mapper.pojo.SmallMiGoodsSku;
import cn.ljp.userLike.mapper.pojo.SmallMiGoodsSpu;
import cn.ljp.userLike.mapper.pojo.SmallMiUserLike;
import cn.ljp.userLike.service.bo.DeleteBo;
import cn.ljp.userLike.service.bo.InsertAddBo;
import cn.ljp.userLike.service.bo.SeleceBo;
import cn.ljp.userLike.service.domain.DeleteDo;
import cn.ljp.userLike.service.domain.InsertAddDo;
import cn.ljp.userLike.service.domain.SeleceDo;
import cn.ljp.userLike.service.service.UserLikeSerice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 描述类
 *
 * @ClassName userLikeSericeImpl
 * @Description TODO
 * @Author liu'jia'peng
 * @Date 2021/7/17 15:24
 * @Version 1.0
 */
@Service
public class UserLikeSericeImpl implements UserLikeSerice {

    @Autowired
    private SmallMiUserLikeMapper smallMiUserLikeMapper;

    @Autowired
    private RedisServie redisServie;

    @Override
    public InsertAddDo insertAdd(InsertAddBo insertAddBo) {
        //先添加到数据库
        //拷贝倒 pojo
        SmallMiUserLike smallMiUserLike = BeanCopierUtil.copyObj(insertAddBo, SmallMiUserLike.class);
        //补充属性
        smallMiUserLike.setCreatedBy(insertAddBo.getMiId());
        smallMiUserLike.setModifieBy(insertAddBo.getMiId());
        smallMiUserLike.setLikeId(new ObjectIdGenerator().next());
        smallMiUserLike.setCreationDate(new Date());
        int i = smallMiUserLikeMapper.insertSelective(smallMiUserLike);
        //拷贝一下
        InsertAddDo insertAddDo = BeanCopierUtil.copyObj(insertAddBo, InsertAddDo.class);
        if (1 == i){

            insertAddDo.setCode("000000");
            return insertAddDo;
        }
        insertAddDo.setCode("999999");
        //返回添加失败
        return insertAddDo;
    }

    @Override
    public DeleteDo delete(DeleteBo deleteBo) {
        //拷贝到pojo
        SmallMiUserLike smallMiUserLike = BeanCopierUtil.copyObj(deleteBo, SmallMiUserLike.class);
        //执行删除
        int i = smallMiUserLikeMapper.deleteUserLike(smallMiUserLike);
        DeleteDo deleteDo = BeanCopierUtil.copyObj(deleteBo, DeleteDo.class);
        if (1==i){
            deleteDo.setCode("000000");
            return deleteDo;
        }
        deleteDo.setCode("999999");
        return deleteDo;
    }


    @Autowired
    private SmallMiGoodsSkuMapper smallMiGoodsSkuMapper;

    @Autowired
    private SmallMiGoodsSpuMapper smallMiGoodsSpuMapper;

    @Override
    public List<SeleceDo> selece(SeleceBo seleceBo) {
        //通过用户 mi_id  查询喜欢喜欢商品信息
        SmallMiUserLike smallMiUserLike = BeanCopierUtil.copyObj(seleceBo, SmallMiUserLike.class);
        List<SmallMiUserLike> smallMiUserLikes = smallMiUserLikeMapper.seleceUserLike(smallMiUserLike);
        List<SeleceDo> list = new ArrayList<>();
        if (smallMiUserLikes.equals(null)){
            return null;
        }
        for (SmallMiUserLike miUserLike : smallMiUserLikes) {
            //创建对象，查询另外两张表的信息
            SeleceDo seleceDo = new SeleceDo();
            SmallMiGoodsSpu smallMiGoodsSpu = new SmallMiGoodsSpu();
            SmallMiGoodsSku smallMiGoodsSku = new SmallMiGoodsSku();
            smallMiGoodsSpu.setGoodsId(miUserLike.getSpuId());
            List<SmallMiGoodsSpu> smallMiGoodsSpus = smallMiGoodsSpuMapper.selectUserLike(smallMiGoodsSpu);
            //查查询商品信息表  要名称，要描述，要类型
            for (SmallMiGoodsSpu miGoodsSpus : smallMiGoodsSpus) {
                seleceDo.setGoodsName(miGoodsSpus.getGoodsName());
                seleceDo.setGoodsComment(miGoodsSpus.getGoodsComment());
                seleceDo.setGoodsType(miGoodsSpus.getGoodsType());
                seleceDo.setGoodsStatus(miGoodsSpus.getGoodsStatus());
            }
            //查询 商品sku表 ， 要商品 规格 库存 价格
            smallMiGoodsSku.setSkuId(miUserLike.getSkuId());
            List<SmallMiGoodsSku> smallMiGoodsSkus = smallMiGoodsSkuMapper.selectUserLike(smallMiGoodsSku);
            for (SmallMiGoodsSku miGoodsSkus : smallMiGoodsSkus) {
                seleceDo.setGoodsStandard(miGoodsSkus.getGoodsStandard());
                seleceDo.setGoodsRepertory(miGoodsSkus.getGoodsRepertory());
                seleceDo.setGoodsPrice(miGoodsSkus.getGoodsPrice());
            }
            list.add(seleceDo);
        }

        return list;
    }
}

